//
//  DetailsController_img.m
//  网易新闻
//
//  Created by dllo on 15/9/25.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "DetailsController_img.h"
#import "Details_imgModel.h"
#import "SDWebImage/UIImageView+WebCache.h"
#import "AFTool.h"
@interface DetailsController_img ()<UIScrollViewDelegate, UIWebViewDelegate>
@property(nonatomic, retain)NSArray *modArr;
@property(nonatomic, retain)NSDictionary *modDic;
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)UILabel *lab;
@property(nonatomic, retain)UILabel *lab2;
@property(nonatomic, retain)UIWebView *web;
@end

@implementation DetailsController_img

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
//    self.navigationController.navigationBar.alpha = 0.1;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    [self handData];

}

- (void)handData{
    
    [AFTool GETWithURL:[NSString stringWithFormat:@"http://c.3g.163.com/photo/api/set/%@.json", self.skipID] body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.modDic = [[NSDictionary alloc]initWithDictionary:result];
        self.modArr = [self.modDic objectForKey:@"photos"];
        [self.modDic release];
        
        [self createScrollView];
        [self createTitle];
        
    } fail:^(NSError *error) {
        NSLog(@"404(DetailsController_img)");
    }];
}

- (void)createScrollView{
    self.scroll = [[UIScrollView alloc]init];
    self.scroll.backgroundColor = [UIColor blackColor];
    self.scroll.pagingEnabled = YES;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator= NO;
    self.scroll.delegate = self;
    [self.view addSubview:self.scroll];
    [self.scroll release];
    
    self.scroll.frame = CGRectMake(0, -64, 375, 667+64);
    self.scroll.contentSize = CGSizeMake(375*self.modArr.count, 1);
    self.scroll.bounces = NO;
    
    for (int i=0; i<self.modArr.count; i++){
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*375, 0, 375, 667)];
        Details_imgModel *mod = [Details_imgModel modWithDic:self.modArr[i]];        
        img.contentMode = UIViewContentModeScaleAspectFit;//高度自适应
        [img sd_setImageWithURL:mod.imgurl placeholderImage:nil];
        [self.scroll addSubview:img];
        [img release];
    }
}

- (void)createTitle{
    self.lab = [[UILabel alloc]initWithFrame:CGRectMake(20, 450, 270, 20)];
    self.lab.backgroundColor = [UIColor clearColor];
    self.lab.text = [self.modDic objectForKey:@"setname"];
    self.lab.textColor = [UIColor whiteColor];
    self.lab.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:self.lab];
    [self.lab release];
    
    self.lab2 = [[UILabel alloc]initWithFrame:CGRectMake(300, 450, 65, 20)];
    self.lab2.backgroundColor = [UIColor clearColor];
    self.lab2.text = [NSString stringWithFormat:@"1/%lu", self.modArr.count];
    self.lab2.textAlignment = NSTextAlignmentRight;
    self.lab2.textColor = [UIColor whiteColor];
    self.lab2.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:self.lab2];
    [self.lab2 release];
    
    self.web = [[UIWebView alloc]init];
    self.web.frame = CGRectMake(10, 490, 355, 120);
    Details_imgModel *mod = [Details_imgModel modWithDic:self.modArr[0]];
    [self.web loadHTMLString: mod.note baseURL:nil];
    self.web.opaque = NO; //不设置这个值 页面背景始终是白色
    self.web.delegate = self;
    [self.view addSubview:self.web];
    [self.web release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    webView.backgroundColor = [UIColor clearColor];
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int a = scrollView.contentOffset.x/375;
    self.lab2.text = [NSString stringWithFormat:@"%d/%lu", a+1, self.modArr.count];
    Details_imgModel *mod = [Details_imgModel modWithDic:self.modArr[a]];
    [self.web loadHTMLString: mod.note baseURL:nil];
}

- (void)dealloc{
    _scroll.delegate = nil;
    _web.delegate = nil;
    [_scroll release];
    [_skipID release];
    [_lab release];
    [_modArr release];
    [_modDic release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
